--
-- Created by IntelliJ IDEA.
-- User: WenYF
-- Date: 2019/1/5 0005
-- Time: 下午 10:49
-- To change this template use File | Settings | File Templates.
--
local FindPath = import("app.model.battlefield.AStarFindPath")

local TestScene = {}

--local map = {
--    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
--    {0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
--    {0,1,0,0,0,1,1,1,1,1,1,1,1,0,1,0},
--    {0,1,1,1,1,1,0,1,1,1,1,1,1,0,1,0},
--    {0,0,0,0,0,1,0,1,1,0,0,0,1,0,1,0},
--    {0,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0},
--    {0,1,1,1,0,1,1,1,1,1,0,0,0,0,1,0},
--    {0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0},
--    {0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0},
--    {0,1,1,0,0,1,1,0,0,0,0,0,1,1,1,0},
--    {0,1,1,1,1,1,1,0,3,1,1,0,1,1,1,0},
--    {0,0,0,0,1,1,1,0,1,1,1,0,1,1,1,0},
--    {0,1,1,1,1,1,1,0,1,1,1,0,1,1,1,0},
--    {0,1,0,1,1,0,0,0,1,1,1,0,1,1,1,0},
--    {0,1,0,0,0,1,1,1,1,0,0,0,1,1,1,0},
--    {0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0},
--    {0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0},
--    {0,1,1,1,1,0,1,1,0,1,1,0,1,1,1,0},
--    {0,1,1,1,1,0,1,1,0,1,1,0,0,0,1,0},
--    {0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0},
--    {0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0},
--    {0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0},
--    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
--}

local map = {
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    {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1,1,1,1,0,1,1,1,0,1,1,0,0},
    {0,0,0,0,1,1,1,0,1,1,1,0,1,1,1,1,1,0,0,0,1,1,1,0,1,1,1,0,1,0,1,0},
    {0,1,1,1,1,1,1,0,1,1,1,0,1,0,0,0,0,0,1,1,1,1,1,0,1,1,1,0,1,0,1,0},
    {0,1,0,1,1,0,0,0,1,1,1,0,1,1,1,1,1,1,0,1,1,0,0,0,1,1,1,0,0,0,1,0},
    {0,1,0,0,0,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0,0,1,1,1,0},
    {0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,1,0,1,1,1,1,0,0,0,1,0,1,1,1,0},
    {0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0},
    {0,0,1,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,1,0,0,1,0,1,1,0,1,1,1,0},
    {0,1,1,1,1,0,1,1,0,1,1,0,0,0,1,0,1,1,0,1,1,0,1,1,0,1,1,0,0,0,1,0},
    {0,0,0,0,1,0,1,1,0,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,1,1,1,0},
    {0,1,1,1,1,0,1,1,0,1,1,1,0,0,0,0,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0},
    {0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0},
    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
}

-- map 倒转一下
for i=1, math.floor(#map/2) do 
    local tmp = map[#map-i+1]   
    map[#map-i+1] = map[i]
    map[i] = tmp
end


function TestScene:show(scene)
    scene:setContentSize({width=win.width,height=win.height})
    local bkSprite = display.newSprite("backgurand.jpg")
    bkSprite:setPosition(win.width/2,win.height/2)
    local rect = bkSprite:getTextureRect()
    bkSprite:setScaleX(win.width/rect.width)
    bkSprite:setScaleY(win.height/rect.height)
    bkSprite:addTo(scene)

    local sceneSize = scene:getContentSize()
    local xe = sceneSize.width / #map[1]
    local ye = sceneSize.height / #map
    local mapUI = cc.DrawNode:create()
    for r = 1, #map do
        mapUI:drawSegment({x=0, y=r*ye}, {x=sceneSize.width, y=r*ye }, 0.5, cc.c4b(0,55,55, 222))
    end

    for c = 1, #map do
        mapUI:drawSegment({x=c*xe, y=0}, {x=c*xe, y=sceneSize.height}, 0.5, cc.c4b(0,55,55, 222))
    end

    local positionA = {}
    local targetB = {}
    for r = 1, #map do
        for c = 1, #map[r] do
            if map[r][c] == 1 then
                mapUI:drawSolidRect({x=(c - 1) * xe, y=(r - 1) * ye}, {x=c * xe, y=r * ye}, cc.c4b(0,0,0,0))
            elseif map[r][c] == 2 then
                positionA.x = c
                positionA.y = r
                mapUI:drawSolidRect({x=(c - 1) * xe, y=(r - 1) * ye}, {x=c * xe, y=r * ye}, cc.c4b(122,11,33,128))
            elseif map[r][c] == 3 then
                targetB.x = c
                targetB.y = r
                mapUI:drawSolidRect({x=(c - 1) * xe, y=(r - 1) * ye}, {x=c * xe, y=r * ye}, cc.c4b(205,11,125,77))
            elseif map[r][c] == 0 then
                mapUI:drawSolidRect({x=(c - 1) * xe, y=(r - 1) * ye}, {x=c * xe, y=r * ye}, cc.c4b(0,255,255,100))
            end
        end
    end

    mapUI:addTo(scene)

    local mapUI = cc.DrawNode:create()
    mapUI:addTo(scene)
    self.mapUI= mapUI

    cc.exports.scene = scene
    local show = true
    if show or false then
        local close = FindPath:find(positionA, targetB, map, function (close, open)
                logW("close : ---------------> ")
                self:drawClose(close, open)
        end, 0.01, show)
    else
        local time1 = os.clock()
        local close = FindPath:find(positionA, targetB, map, nil, 0.1, show)
        logW("find path time : " .. (os.clock() - time1))
        TestScene:drawClose(close)
    end
end

function TestScene:drawClose(close, open)
    local mapUI = self.mapUI
    mapUI:clear()
    local sceneSize = scene:getContentSize()
    local xe = sceneSize.width / #map[1]
    local ye = sceneSize.height / #map
    for k,v in pairs(close) do
        mapUI:drawSolidRect({x=(v.x - 1) * xe, y=(v.y - 1) * ye}, {x=v.x * xe, y=v.y * ye}, cc.c4b(66,88,11,77))
    end

    open = open or {}
    for k,v in pairs(open) do
        mapUI:drawSolidRect({x=(v.x - 1) * xe, y=(v.y - 1) * ye}, {x=v.x * xe, y=v.y * ye}, cc.c4b(66,88,11,188))
    end
end

return TestScene

